package com.eight.cloud.uid.config;

import com.eight.cloud.uid.core.impl.CachedUidGenerator;
import com.eight.cloud.uid.core.impl.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 默认的生成器策略
 *
 * @ClassName: DefaultGenerator
 * @Author: TXC
 * @Date: 2025-09-09 10:17
 **/
@Slf4j
@Component
@ConditionalOnMissingBean(CachedGenerator.class)
@AutoConfigureAfter(value = { UidGeneratorConfiguration.class })
public class DefaultGenerator implements UidGeneratorService {
    @Resource
    private DefaultUidGenerator defaultUidGenerator;

    /**
     * 获取uid
     *
     * @return 生成的uid
     */
    @Override
    public long nextId() {
        long uid = defaultUidGenerator.getUID();
        log.info("DefaultGenerator nextId uid:{}", uid);
        return uid;
    }

    /**
     * 格式化传入的uid，方便查看其实际含义
     *
     * @param uid 要格式化的uid
     * @return 格式化后的uid
     */
    @Override
    public String parse(long uid) {
        String parseUID = defaultUidGenerator.parseUID(uid);
        log.info("DefaultGenerator parse uid:{}", parseUID);
        return parseUID;
    }
}
